package univ.paris.ouest.data.user;

import java.util.List;


import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;

import univ.paris.ouest.domaine.user.User;

public class UserDaoHib implements IUserDao{
	private Session session;
	
	@Override
	public void saveUser(User user) {
		this.session.save(user);	
	}

	@Override
	public List<User> getAllUsers() {
		Query query = this.session.createQuery("From User");
		return (List<User>)query.list();
	}

	@Override
	public void setSession(Object session) {
		this.session= (Session) session;
	}

	@Override
	public User getUser(String nom, String prenom) {
		Query query = this.session.createQuery("From User where NOM=? AND PRENOM=?");
		query.setParameter(0,nom,Hibernate.STRING);
		query.setParameter(1,prenom,Hibernate.STRING);
		
		return (User) query.list().get(0);
	}

	@Override
	public void upDateUser(User user) {
		this.session.update(user);
	}

	@Override
	public void deleteUser(User user) {
		this.session.delete(user);
	}

	@Override
	public User getUser(User user) {
		Query query = this.session.createQuery("From User where LOGIN_ID =? ");
		query.setParameter(0,user.getUserLogin().getEmail(),Hibernate.STRING);
		return (User) query.list().get(0);
	}
}
